package com.ychug.front.template.freemarker;

import com.ychug.framework.util.ServletUtils;
import com.ychug.front.config.FrontConst;
import com.ychug.front.template.directive.CustomTagDirective;
import com.ychug.front.template.directive.SiteTagDirective;
import freemarker.template.TemplateModelException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 模板配置信息
 * dds
 * 2019-04-19
 */
@Configuration
public class FreeMarkerConfig {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    public freemarker.template.Configuration configuration;

    @Autowired
    protected CustomTagDirective customTagDirective;
    @Autowired
    protected SiteTagDirective siteTagDirective;


    public Map getFreemarkerMap(){
        Map data = new HashMap();
        data.put("like",siteTagDirective);
        data.put("ad",customTagDirective);
        return data;
    }

    @PostConstruct
    public void freemarkerInit(){
        try {
            configuration.setSharedVariable(FrontConst.DIRECTIVE_PREFIX, getFreemarkerMap());
        } catch (TemplateModelException e) {
            e.printStackTrace();
        }

        // •AUTO_DETECT_TAG_SYNTAX: 可以自定义格式，系统会选择第一个标签作为参照；
        // •ANGLE_BRACKET_TAG_SYNTAX: 使用尖括号；
        // •SQUARE_BRACKET_TAG_SYNTAX: 使用方括号；
        configuration.setTagSyntax(freemarker.template.Configuration.AUTO_DETECT_TAG_SYNTAX);
        configuration.setWhitespaceStripping(true);
        configuration.setClassicCompatible(true);

        configuration.setNumberFormat("0.######");
        configuration.setBooleanFormat("true,false");
        configuration.setDateTimeFormat("yyyy-MM-dd HH:mm:ss");
        configuration.setDateFormat("yyyy-MM-dd");
        configuration.setTimeFormat("HH:mm:ss");
    }

    public freemarker.template.Configuration getConfiguration() {
        return configuration;
    }

    public void setConfiguration(freemarker.template.Configuration configuration) {
        this.configuration = configuration;
    }

//    @PostConstruct
//    public void setSharedVariable() {
//        // icbcTag即为页面上调用的标签名
//        configuration.setSharedVariable("mcp_ad", siteTagDirective);
//    }
}

